package com.flismax.mysms.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.flismax.mysms.R;
import com.flismax.mysms.util.MyToast;
import com.flismax.mysms.util.Sms;
import com.flismax.mysms.util.SmsUtil;

public class MySmsActivity extends Activity implements OnClickListener {

	private EditText phoneEditText;
	private EditText bodyEditText;
	private Sms sms;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_sms);

		phoneEditText = (EditText) findViewById(R.id.phoneEditText);
		bodyEditText = (EditText) findViewById(R.id.bodyEditText);

		((Button) findViewById(R.id.insertButton)).setOnClickListener(this);
		((Button) findViewById(R.id.deleteButton)).setOnClickListener(this);
		((Button) findViewById(R.id.clearButton)).setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.insertButton:
			insertSms();
			break;
		case R.id.deleteButton:
			deleteSms();
			break;
		case R.id.clearButton:
			clearButton();
			break;
		default:
			break;
		}
	}

	private void insertSms() {
		sms = new Sms();
		sms.setAddress(phoneEditText.getText().toString());
		sms.setBody(bodyEditText.getText().toString());
		sms.setType(Sms.TextBasedSmsColumns.MESSAGE_TYPE_INBOX);
		sms.setDate(System.currentTimeMillis());
		boolean result = SmsUtil.insert(this, Sms.INBOX_CONTENT_URI, sms);
		if (result) {
			MyToast.show(this, "insert success");
		} else {
			MyToast.show(this, "insert fail");
		}
	}

	private void deleteSms() {
		sms = new Sms();
		sms.setAddress(phoneEditText.getText().toString());
		sms.setBody(bodyEditText.getText().toString());
		sms.setType(Sms.TextBasedSmsColumns.MESSAGE_TYPE_INBOX);
		boolean result = SmsUtil.deleteConversations(this, sms.getAddress());
		if (result) {
			MyToast.show(this, "delete success");
		} else {
			MyToast.show(this, "delete fail");
		}
	}

	private void clearButton() {
		boolean result = SmsUtil.clearConversations(this);
		if (result) {
			MyToast.show(this, "clear success");
		} else {
			MyToast.show(this, "clear fail");
		}
	}

}
